/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       lighthouse.cpp

	$Header: /game/lighthouse.cpp $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "lighthouse.h"

#include "adv_object_type.h"
#include "adventure_ai.h"
#include "adventure_map.h"
#include "army.h"
#include "creature_array.h"
#include "object_registration.h"
#include "player.h"
#include "simple_dialog.h"
#include "sized_int_types.h"
#include "streambuf_operators.h"

// ---------------------------------------------------------------
// unnamed namespace
// ---------------------------------------------------------------

namespace
{

	t_object_registration< t_lighthouse > g_lighthouse_registration( k_adv_object_lighthouse );

	// ---------------------------------------------------------------
	// ---------------------------------------------------------------
	inline int to_base_map_format_version( int format_version )
	{
		return format_version;
	}

} // unnamed namespace

// ---------------------------------------------------------------
// lighthouse adventure object
// ---------------------------------------------------------------
bool t_lighthouse::read_from_map(
	std::streambuf &		buffer,
	t_progress_handler *	handler )
{
	int const k_current_format_version = 2;

	int format_version = get< t_uint16 >( buffer );
	if ( format_version < 0 || format_version > k_current_format_version )
		return false;

	return	t_owned_scriptable_adv_object::read_from_map(
				buffer,
				handler,
				to_base_map_format_version( format_version ) );
}

// ---------------------------------------------------------------
// ---------------------------------------------------------------
void t_lighthouse::activate_trigger( t_army* army, t_adv_map_point const& point, 
		                             t_direction direction, t_adventure_frame* frame )
{
	t_player* army_owner = army->get_owner();	
	
	// Claim lighthouse 
	std::string text;

	if ( army_owner == get_owner() )
	{
		text = get_text( "empty");
	}
	else if ( is_ally( army_owner, get_owner() ) )
	{
		text = get_text( "refused_ally" );
	}
	else if ( army->get_living_hero_count() == 0 )
		text = get_text( "no_heroes" );
	else
	{
		set_owner( army->get_owner_number() );
		text = get_text( "initial" );
	}

	if ((army_owner != NULL) && !army_owner->is_computer())
		ok_dialog( text, get_dialog_bottom( point ), get_name() );
}


float t_lighthouse::ai_value( t_adventure_ai const& ai, t_creature_array const& army, int move_cost ) const
{
	if (get_owner() == army.get_owner())
		return 0.0f;

	if (army.get_living_hero_count() == 0)
		return 0.0f;
	
	// The multiplier is 4/30...  The default sea move speed is 15.  A doubling of
	// movespeed is a 50% army value increase.
	return 0.1333f * (1.0f - ai.get_land_coverage()) * ai.scale_for_aggression(get_owner(), army.get_owner()) * army.ai_value();
}


// -----------------------------------------------------------
// keep ownership info in player up to date
// -----------------------------------------------------------
void t_lighthouse::place( t_adventure_map& map, t_adv_map_point const& point )
{
	t_owned_scriptable_adv_object::place( map, point );

	if (get_owner() != 0)
		get_owner()->add_lighthouse( this );
}

void t_lighthouse::set_owner( int player_number )
{
	if (get_owner() != 0)
		get_owner()->remove_lighthouse( this );
	t_owned_scriptable_adv_object::set_owner( player_number );
	if (get_owner() != 0)
		get_owner()->add_lighthouse( this );
}
